package com.yao.longhan.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.yao.longhan.R;
import com.yao.longhan.constant.Constant;
import com.yao.longhan.constant.Gender;
import com.yao.longhan.listener.ItemClickListener;
import com.yao.longhan.model.ChannelBook;
import com.yao.longhan.model.StoryArticle;
import com.yao.longhan.net.BaseCallback;
import com.yao.longhan.presenter.adapter.ChannelAdapter;
import com.yao.longhan.ui.activity.BookInfoActivity;

import java.util.List;

import butterknife.BindView;

/**
 * Created by heyao on 2017/7/31.
 */

public class BookcaseItemFragment extends LazyLoadFragment implements SwipeRefreshLayout.OnRefreshListener, ItemClickListener {

    @BindView(R.id.srl_bookcase_item)
    SwipeRefreshLayout mRefreshLayout;

    @BindView(R.id.rv_bookcase_item)
    RecyclerView mRecyclerView;

    private ChannelAdapter mAdapter;
    private int REQUEST_CHANNEL = 0x001;
    private String channel;
    private int page;

    public static BookcaseItemFragment instance(Bundle bundle) {
        BookcaseItemFragment fragment = new BookcaseItemFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_bookcase_item;
    }

    @Override
    protected void initBefore() {
        super.initBefore();
        int position = arguments.getInt(Constant.CHANNEL);
        if (position == 0) {
            channel = Gender.MALE.getCode();
        } else {
            channel = Gender.FEMALE.getCode();
        }
        Log.e(TAG, "initBefore: " + channel);
    }

    @Override
    protected void initAfter() {
        Log.e(TAG, "initAfter: " + channel);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new ChannelAdapter(mApp);
        mAdapter.setClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setOnRefreshListener(this);
        super.initAfter();
    }

    @Override
    public void onRefresh() {
        page = 0;
        mAdapter.clearData();
        mApp.getRetrofitService().getChannelBook(0, channel, page).enqueue(new BaseCallback<List<ChannelBook>>(this, REQUEST_CHANNEL));
    }

    @Override
    public void onFailed(int requestCode, String message) {
        super.onFailed(requestCode, message);
        if (requestCode == REQUEST_CHANNEL) {
            if (mRefreshLayout != null) {
                mRefreshLayout.setRefreshing(false);
            }
        }
    }

    @Override
    public void onSuccess(int requestCode, Object object) {
        super.onSuccess(requestCode, object);
        if (requestCode == REQUEST_CHANNEL) {
            List<ChannelBook> channelBooks = (List<ChannelBook>) object;
            for (ChannelBook channelBook : channelBooks) {
                List<StoryArticle> storyArticle = channelBook.getStoryArticle();
                mAdapter.addData(storyArticle);
            }
            mAdapter.notifyDataSetChanged();
            if (mRefreshLayout != null) {
                mRefreshLayout.setRefreshing(false);
            }
            Log.e(TAG, "ItemCount: " + mAdapter.getItemCount());
        }
    }

    @Override
    protected void stopLoad() {
        mAdapter.clearData();
        Log.e(TAG, "stopLoad: " + channel);
    }

    @Override
    protected void lazyLoad() {
        Log.e(TAG, "lazyLoad: " + channel);
        mRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        });
    }

    @Override
    public void clickEvent(Bundle bundle) {
        toActivity(BookInfoActivity.class, bundle);
    }
}
